﻿
Partial Class WebForm_ManageRole_SearchRoleOrganizationMappingByRole
    Inherits BaseUserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Visible = True

        If Not IsPostBack Then
        End If
    End Sub

    Protected Sub chkAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chkAll As CheckBox = TryCast(sender, CheckBox)

        For Each row As GridViewRow In Me.gv.Rows
            Dim chk As CheckBox = TryCast(row.FindControl("chkAdmin"), CheckBox)

            chk.Checked = chkAll.Checked
        Next

        Dim cellIndex = Me.gv.HeaderRow.Cells.GetCellIndex(chkAll.Parent)

        Me.AddAllRoleAuthMapping(chkAll)
        Me.Show(Me.RoleID)
    End Sub

    Protected Sub chk_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chk As CheckBox = TryCast(sender, CheckBox)

        Me.AddRoleAuthMapping(chk)
        Me.Show(Me.RoleID)
    End Sub


    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.Show(Me.RoleID)
    End Sub


    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.Show(Me.RoleID)
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim chkAdmin As CheckBox = TryCast(e.Row.FindControl("chkAdmin"), CheckBox)
            Dim chkAllAdmin As CheckBox = TryCast(Me.gv.HeaderRow.FindControl("chkAllAdmin"), CheckBox)
            chkAllAdmin.Checked = chkAllAdmin.Checked And chkAdmin.Checked
        End If
    End Sub
End Class

Partial Class WebForm_ManageRole_SearchRoleOrganizationMappingByRole
    Private authList() As String = {"Admin"}

    ''' <summary>
    ''' 僅顯示，不重載資料
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Show()
        Me.mpSearch.Show()
        Me.Visible = True
    End Sub

    ''' <summary>
    ''' 顯示且重載資料
    ''' </summary>
    ''' <param name="roleID"></param>
    ''' <remarks></remarks>
    Public Sub Show(ByVal roleID As String)
        Me.RoleID = roleID
        Me.lblRoleName.Text = Me.RoleName

   
        Me.BindGV()


        Me.mpSearch.Show()
        Me.Visible = True
    End Sub
    Private Sub BindGV()



        Dim bbl As WorkFlow.BBL.GroupOrgBBL = New WorkFlow.BBL.GroupOrgBBL()
        Me.gv.DataSource = bbl.GetGroupOrgShowData(Me.txtOrganizationName.Text.Trim(), RoleID, dbName)
        Me.gv.DataBind()

        'Dim mappings As Data.DataTable = bbl.GetGroupOrganizationMapping(Me.RoleID, dbName)

        'For Each row As GridViewRow In Me.gv.Rows
        '    Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

        '    For Each auth As String In Me.authList

        '        Dim chkAuth As CheckBox = TryCast(row.FindControl("chk" + auth), CheckBox)
        '        Dim cellIndex = row.Cells.GetCellIndex(chkAuth.Parent)
        '        Dim chkAll As CheckBox = TryCast(Me.gv.HeaderRow.Cells(cellIndex).Controls(1), CheckBox)

        '        mappings.DefaultView.RowFilter = String.Format("OrganizationID = '{0}' ", key("OrganizationID"))

        '        If mappings.DefaultView.Count > 0 Then
        '            chkAuth.Checked = True
        '        Else
        '            chkAuth.Checked = False
        '        End If

        '    Next
        'Next

    End Sub



    Private Function AddRoleAuthMapping(ByVal chkAuth As CheckBox) As Boolean

        Dim bbl As WorkFlow.BBL.GroupOrgBBL = New WorkFlow.BBL.GroupOrgBBL()


        Dim row As GridViewRow = TryCast(chkAuth.NamingContainer, GridViewRow)
        Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)
        'Dim auth As String = Me.FindAuth(chkAuth.ID)

        If chkAuth.Checked = True Then
      
            bbl.InsertGroupOrganizationMapping(Me.RoleID, key("OrganizationID"), ActualUID, False, dbName)
        Else
        
            bbl.DeleteGroupOrganizationMapping(Me.RoleID, key("OrganizationID"), dbName)
        End If


        Return True
    End Function

    Private Function AddAllRoleAuthMapping(ByVal chkAuth As CheckBox) As Boolean

        Dim bbl As WorkFlow.BBL.GroupOrgBBL = New WorkFlow.BBL.GroupOrgBBL()
        'Dim auth = Me.FindAuth(chkAuth.ID)

        For Each row As GridViewRow In Me.gv.Rows
            Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)
            bbl.DeleteGroupOrganizationMapping(Me.RoleID, key("OrganizationID"), dbName)

            If chkAuth.Checked = True Then

                bbl.InsertGroupOrganizationMapping(Me.RoleID, key("OrganizationID"), ActualUID, False, dbName)


            End If
        Next
        'bbl.DeleteGroupOrganizationMapping(Me.RoleID, "", dbName)
        'If chkAuth.Checked = True Then

        '    bbl.InsertGroupOrganizationMapping(Me.RoleID, "", Me.ActualUID, True, dbName)
        'End If

        Return True
    End Function

    'Private Function FindAuth(ByVal searchText As String) As String
    '    For Each auth As String In Me.authList
    '        If searchText.Contains(auth) Then
    '            Return auth
    '        End If
    '    Next

    '    Return ""
    'End Function

    Public Property RoleID() As String
        Get
            Return ViewState("RoleID")
        End Get
        Set(ByVal value As String)
            ViewState("RoleID") = value
            Dim bbl As WorkFlow.BBL.GroupOrgBBL = New WorkFlow.BBL.GroupOrgBBL()
            Me.RoleName = bbl.GetGroupName(Me.RoleID, dbName)
        End Set
    End Property

    Public Property RoleName() As String
        Get

            Return ViewState("RoleName")
        End Get
        Set(ByVal value As String)
            ViewState("RoleName") = value
        End Set
    End Property
End Class